from django.shortcuts import render
from .models import Project,Person,Address,Author,Book
from rest_framework.decorators import api_view
from rest_framework.parsers import JSONParser
from django.http import JsonResponse
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
import logging
# Create your views here.

@api_view(['POST'])
def addauthor(request):
    data=JSONParser().parse(request)
    name=data.get('name')
    gender=data.get('gender')
    age=data.get('age')
    Author(name=name,age=age,gender=gender).save()
    return JsonResponse({
        'code':200,
        'msg':'添加成功'
    })
@api_view(['POST'])
def addbook(request):
    data=JSONParser().parse(request)
    name=data.get('name')
    price=data.get('price')
    publisher=data.get('publisher')
    # author_id=data.get('author_id')
    author_name=data.get('author_name')
    # 方法1:根据id添加
    # Book(name=name,price=price,publisher=publisher,author_id=author_id).save()
    # 先得到作者对象
    authorobj=Author.objects.get(name=author_name)
    # 方法2: 插入数据(根据姓名添加)
    Book(name=name,price=price,publisher=publisher,author=authorobj).save()
    return JsonResponse({
        'code': 200,
        'msg': '添加成功'
    })

#根据作者姓名查询书籍
@api_view(['GET'])
def getallinfo(request):
    name=request.GET.get('author_name')
    # objects负责数据库查询····
    authorobj=Author.objects.get(name=name)
    # 运用到models里的related_name='book'
    bookobj=authorobj.book.all()
    logging.info(f'查看作者关联出来的所有书籍对象:{bookobj}')
    lis=[]
    for item in bookobj:
        lis.append({
            '作者':authorobj.name,
            '年龄':authorobj.age,
            '性别':authorobj.gender,
            '书名':item.name,
            '价格':item.price,
            '穿案涉':item.publisher
        })
    return JsonResponse({
        'code':200,
        'data':lis,
        'msg':'查询作者书籍成功'
    })

@api_view(['GET'])
def bookselectauthor(request):
    name=request.GET.get('name')
    bookobj=Book.objects.get(name=name).first()
    return JsonResponse({
        'code':200,
        'bookname':name,
        # 通过书籍的对象关联到作者的对象
        '作者姓名':bookobj.author.name,
        '作者性别':bookobj.author.gender,
        '作者年龄':bookobj.author.age,
    })







# 添加人员Person
@api_view(['POST'])
def addperson(request):
    data=JSONParser().parse(request)
    name=data.get('name')
    age=data.get('age')
    gender=data.get('gender')
    Person(name=name,age=age,gender=gender).save()
    return JsonResponse({
        'code':200,
        'msg':'添加成功'
    })

# 添加人员Address
@api_view(['POST'])
def addaddress(request):
    data=JSONParser().parse(request)
    person_id=data.get('person_id')
    city=data.get('city')
    town=data.get('town')
    village=data.get('village')
    detailaddress=data.get('detailaddress')
    Address(person_id=person_id,city=city,town=town,village=village,detailaddress=detailaddress).save()
    return JsonResponse({
        'code':200,
        'msg':'添加成功'
    })



@api_view(['POST'])
def addproject(request):
    data=JSONParser().parse(request)
    name=data.get('name')
    status=data.get('status')
    desc=data.get('desc')
    Project(name=name,status=status,desc=desc).save()
    return JsonResponse({
        'code':200,
        'msg':'添加项目成功'
    })

@api_view(['GET'])
def getallproject(request):
    datas=Project.objects.filter()
    logging.info(f"看看查出的所有数据:{datas}")
    lis=[]
    for data in datas:
        lis.append({
            '项目名':data.name,
            "状态":data.status,
            "项目描述":data.desc
        })
    return JsonResponse({
        'code':200,
        "msg":"查询成功",
        'data':lis
    })

@api_view(['DELETE'])
def delete(request):
    data=JSONParser().parse(request)
    name=data.get('name')
    d=Project.objects.filter(name=name).first()
    d.delete()
    return JsonResponse({
        'code':200,
        'msg':'删除成功'
    })

@api_view(['PUT'])
def update(request):
    data = JSONParser().parse(request)
    name = data.get('name')
    desc = data.get('desc')
    d=Project.objects.filter(name=name)
    if d.exists() is False:
        return JsonResponse({
            'code':400,
            'msg':'项目不存在'
        },status=400)
    d=Project.objects.get(name=name)
    d.desc=desc
    d.save()
    logging.info(f'查看数据{d}')
    return JsonResponse({
        'code': 200,
        'msg': '修改描述成功'
    }, status=200)

# 分页
@api_view(['GET'])
def fenye(request):
    page=request.GET.get('page')
    pagesize=request.GET.get('pagesize')
    datas=Project.objects.filter().values()
    pageobj=Paginator(datas,pagesize)
    try:
        danyeobj=pageobj.page((page))
    except EmptyPage:
        danyeobj=pageobj.page(pageobj.num_pages)
    except PageNotAnInteger:
        danyeobj=pageobj.page(1)
    lis=[]
    for item in danyeobj.object_list:
        lis.append({
            '项目名':item.get('name'),
            '项目状态':item.get('status'),
            '项目描述':item.get('desc'),
        })
    return JsonResponse({
        'code':200,
        'msg':'请求成功',
        'data':lis
    })

@api_view(['GET'])
def getmohu(request):
    name=request.GET.get('name')
    datas=Project.objects.filter(name__contains=name)
    logging.info(f'查看获取的数据:{datas}')
    lis=[]
    for item in datas:
        logging.info(f"查看item:{item}")
        lis.append({
            'person_id':item.id,
            'person_name':item.name
        })
    return JsonResponse({
        'code':200,
        'msg':'查询成功',
        'data':lis
    })
